package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DroleDao;
import cn.gson.fengboot.model.dao.DrolesmenuDao;
import cn.gson.fengboot.model.mapper.DempMapper;
import cn.gson.fengboot.model.mapper.DloginMapper;
import cn.gson.fengboot.model.pojos.QempFrom;
import cn.gson.fengboot.model.pojos.QmenusEntity;
import cn.gson.fengboot.model.pojos.QroleFrom;
import cn.gson.fengboot.model.pojos.Qrolesmenu;
import cn.gson.fengboot.model.routervo.Router;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DloginService {
    @Autowired
    DloginMapper dloginMapper;

    @Autowired
    DrolesmenuDao drolesmenuDao;

    @Autowired
    DroleDao droleDao;

    @Autowired
    DempMapper dempMapper;

    public Collection<QmenusEntity> fublist(String empname) {
        return dloginMapper.fun(empname);
    }

    //根据员工角色查找对应查看权限
    public Integer getliro(Integer roleId){
        List<Qrolesmenu> byRoleId = drolesmenuDao.findByRoleId(roleId);//得到了员工权限组
        return byRoleId.get(0).getLiro().getLiId();//得到查看权限id
    }

     //获取员工姓名集合
    public List<String> getbuspeoplelist(HttpServletRequest request){
        QempFrom emp=(QempFrom)request.getSession().getAttribute("user");//取session
        Integer roleId = emp.getRoleId().getRoleId();//得到员工的权限
        Integer getliro = getliro(roleId);//根据员工角色查找对应查看权限
        List<QempFrom> emplist = null;
        List<String> empnamelist = null;
        empnamelist = new ArrayList<>();
        emplist = new ArrayList<>();
        if(getliro==1){//查看自己和下属权限
            List<QroleFrom> byRoleUpId = droleDao.findByRoleUpId(roleId);//查找所有下属

            for (int i=0;i<byRoleUpId.size();i++){
                List<QempFrom> qempFroms = dempMapper.findallByRoleId(byRoleUpId.get(i).getRoleId());
                emplist.addAll(qempFroms);//将下属emp信息添加进集合
            };

        }else if(getliro==2){//所有人
            return empnamelist;//返回null
        }else if(getliro==3){//仅自己
            emplist.add(emp);
        }else if(getliro==4){//部门所有人
            List<QempFrom> qempFroms = dempMapper.findallBydeptId(emp.getDepId().getDepId());
            emplist.addAll(qempFroms);//将本部门emp信息添加进集合
        }

        for(int k=0;k<emplist.size();k++){
            empnamelist.add(emplist.get(k).getEmpName());//添加员工名称
        }
        empnamelist.add(emp.getEmpName());//将自己添加集合里

        empnamelist = this.removename(empnamelist);//去重

        return empnamelist;
    };

    //获取员工姓名集合
    public List<Integer> getempidlist(HttpServletRequest request){
        QempFrom emp=(QempFrom)request.getSession().getAttribute("user");//取session
        Integer roleId = emp.getRoleId().getRoleId();//得到员工的权限
        Integer getliro = getliro(roleId);//根据员工角色查找对应查看权限
        List<QempFrom> emplist = null;
        List<Integer> empnamelist = null;
        empnamelist = new ArrayList<>();
        emplist = new ArrayList<>();
        if(getliro==1){//查看自己和下属权限
            List<QroleFrom> byRoleUpId = droleDao.findByRoleUpId(roleId);//查找所有下属

            for (int i=0;i<byRoleUpId.size();i++){
                List<QempFrom> qempFroms = dempMapper.findallByRoleId(byRoleUpId.get(i).getRoleId());
                emplist.addAll(qempFroms);//将下属emp信息添加进集合
            };

        }else if(getliro==2){//所有人
            return empnamelist;//返回null
        }else if(getliro==3){//仅自己
            emplist.add(emp);
        }else if(getliro==4){//部门所有人
            List<QempFrom> qempFroms = dempMapper.findallBydeptId(emp.getDepId().getDepId());
            emplist.addAll(qempFroms);//将本部门emp信息添加进集合
        }

        for(int k=0;k<emplist.size();k++){
            empnamelist.add(emplist.get(k).getEmpId());//添加员工名称
        }
        empnamelist.add(emp.getEmpId());//将自己添加集合里

        empnamelist = this.removeId(empnamelist);//去重

        return empnamelist;
    };



    /*姓名去重*/
    public List<String> removename(List<String> arr){
        /*去重方法*/
        for(int i=0;i<arr.size()-1;i++){
            for(int j=i+1;j<arr.size();j++){
                if(arr.get(i).equals(arr.get(j))){
                    arr.remove(j);
                }
            }
        }
        return arr;
    };

    /*ID去重*/
    public List<Integer> removeId(List<Integer> arr){
        /*去重方法*/
        for(int i=0;i<arr.size()-1;i++){
            for(int j=i+1;j<arr.size();j++){
                if(arr.get(i).equals(arr.get(j))){
                    arr.remove(j);
                }
            }
        }
        return arr;
    };

    public QempFrom login(String phone, String pwd) {
        return dloginMapper.login(phone, pwd);
    }

    public List<Router> myemprouter(String getbuspeople) {
        List<Router> mydefaultrouter = dloginMapper.mydefaultrouter();
        List<Router> myemprouter = dloginMapper.myemprouter(getbuspeople);
        for(Router v : mydefaultrouter){//添加个人默认路由
            myemprouter.get(0).getChildren().add(v);
        }
        return myemprouter;
    }

    public List<String> mybuttonrouter(Integer empId) {
        return  dloginMapper.mybuttonrouter(empId);
    }
}
